"""
这是操作日志页面类
"""
#引入基类
from framwork.pages.base_page import BasePage
#引入by方法
from selenium.webdriver.common.by import By
#引入下拉框操作方法
from selenium.webdriver.support.select import Select
#引入断言失败异常
from selenium.common.exceptions import NoSuchElementException

class OperationLogPage(BasePage):
    """先写定位器，再写方法"""
    add_operation_locator = (By.LINK_TEXT, "添加")   #
    delete_operation_locator = (By.LINK_TEXT, "删除")
    edit_operation_locator = (By.LINK_TEXT, "编辑")

    select_onchange_locator = (By.ID, "field")      #搜素条件下拉框
    search_locator = (By.NAME, "search")    #搜索输入框
    #搜索按钮
    search_btn_locator = (By.CSS_SELECTOR, "#searchForm > ul > li:nth-child(4) > button")
    #添加操作，列表中的内容
    add_content_locator = (By.CSS_SELECTOR, "#form1 > table > tbody > tr:nth-child(1) > td:nth-child(4)")
    #删除操作，列表中的内容
    delete_content_locator = (By.XPATH, "/html/body/div[5]/div[2]/div[3]/form/table/tbody/tr[1]/td[4]")
    #编辑操作，列表中的内容
    edit_content_locator = (By.XPATH, "/html/body/div[5]/div[2]/div[3]/form/table/tbody/tr[1]/td[4]")
    #按操作人查询，列表中的第一条内容，若存在
    pp_content_locator = (By.CSS_SELECTOR, "#form1 > table > tbody > tr:nth-child(1) > td:nth-child(4)")

    def click_edit_operation(self):
        '''点击编辑，按编辑操作查询'''
        self.find_element(self.edit_operation_locator).click()

    def click_delete_operation(self):
        '''点击删除，按删除操作查询'''
        self.find_element(self.delete_operation_locator).click()

    def click_add_operation(self):
        '''点击添加，按添加操作查询操作日志'''
        self.find_element(self.add_operation_locator).click()

    def drop_down_box(self):
        '''操作人下拉框'''
        select = Select(self.find_element(self.select_onchange_locator))
        select.select_by_value("content")

    def input_search_text(self, search_text):
        '''输入搜索条件'''
        self.find_element(self.search_locator).send_keys(search_text)

    def click_search(self):
        """点击搜索按钮"""
        self.find_element(self.search_btn_locator).click()

    def add_content(self):
        '''添加，列表内容文本，用于断言'''
        try:
            text = self.find_element(self.add_content_locator).text
        except NoSuchElementException:
            text = ""
        return text

    def delete_content(self):
        '''删除；列表内容文本，用于断言'''
        try:
            text = self.find_element(self.delete_content_locator).text
        except NoSuchElementException:
            text = ""
        return text

    def edit_content(self):
        '''编辑；列表内容文本，用于断言'''
        try:
            text = self.find_element(self.edit_content_locator).text
        except NoSuchElementException:
            text = ""
        return text

    def pp_content(self):
        '''按操作人查询，列表中的第一条内容，若存在，用于断言'''
        try:
            text = self.find_element(self.pp_content_locator).text
        except NoSuchElementException:
            text = ""
        return text

    def accept_alert(driver):
        try:
            super().switch_to_alert_accpet()
            return True
        except:
            return False
        # 成功返回 True，失败返回 False




